import com.itheima.account.dao.IAccountDao;
import com.itheima.account.service.IAccountService;
import com.itheima.account.service.impl.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AccountTest1 {
    @Test
    public void Test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test1.xml");
        IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
        accountService.saveAccount();
    }

    @Test
    public void Test2() {
        //测试根据id获取bean 和 根据类型获取bean,结果获取的是同一个bean,因为是一个实例
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test2.xml");
        MyService2 myService2 = (MyService2) applicationContext.getBean("myService2");
        System.out.println("根据id获取" + myService2);
        MyService2 myService21 = applicationContext.getBean("myService2", MyService2.class);
        System.out.println("根据类型获取" + myService21);
    }

    @Test
    public void Test3() {
        //测试多例,结果bean不相同
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test3.xml");
        MyService3 bean1 = applicationContext.getBean("myService3", MyService3.class);
        System.out.println(bean1);
        MyService3 bean2 = applicationContext.getBean("myService3other", MyService3.class);
        System.out.println(bean2);
    }

    @Test
    public void Test4() {
        //测试bean初始化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test4.xml");
        MyService4 myService4 = applicationContext.getBean("myService4", MyService4.class);
        System.out.println(myService4.isInitialized());
    }

    @Test
    public void Test5() {
        //测试bean销毁
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("test5.xml");
        MyService5 myService5 = applicationContext.getBean("myService5", MyService5.class);
        applicationContext.close();
        System.out.println(myService5.isDestroyed());
    }

    @Test
    public void Test6() {
        //静态工厂方法
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test6.xml");
        MyService6 bean = applicationContext.getBean(MyService6.class);
        bean.sayResult();
    }

    @Test
    public void Test7(){
        //实例工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test7.xml");
        MyService7 bean = applicationContext.getBean(MyService7.class);
        bean.sayResult();
    }

    @Test
    public void Test8(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test8.xml");
        //一定要通过id获取bean
        MyService8 bean = (MyService8) applicationContext.getBean("myService8");
        bean.sayResult();
    }

    @Test
    public void Test9(){
        //构造注入
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test9.xml");
        MyService9 myService9 = applicationContext.getBean("myService9", MyService9.class);
        System.out.println(myService9.toString());
    }

    @Test
    public void Test10(){
        //set注入
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test10.xml");
        MyService10 myService10 = applicationContext.getBean("myService10", MyService10.class);
        System.out.println(myService10.toString());
    }

    @Test
    public void Test11(){
        //constructor自动匹配,在xml文件中自动寻找构造方法参数的类型的bean
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test11.xml");
        MyService11 myService11 = applicationContext.getBean("myService11", MyService11.class);
        System.out.println(myService11.toString());
    }

    @Test
    public void Test12(){
        //byType自动匹配,根据属性的类型进行匹配
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test12.xml");
        MyService12 myService12 = applicationContext.getBean("myService12", MyService12.class);
        System.out.println(myService12.toString());
    }

    @Test
    public void Test13(){
        //byName自动匹配,根据属性的类型进行匹配
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test13.xml");
        MyService13 myService13 = (MyService13) applicationContext.getBean("myService13");
        System.out.println(myService13.toString());
    }
}
